<template>
  <div class="nav-bar van-hairline--top">
    <ul class="nav-list">
      <router-link class="nav-list-item active" to="home">
        <van-icon name="wap-home-o" />
        <span>首页</span>
      </router-link>
      <router-link class="nav-list-item" to="cart">
        <van-icon name="shopping-cart-o" :badge="!cartItemCount ? '' : cartItemCount" />
        <span>购物车</span>
      </router-link>
      <router-link class="nav-list-item" to="mine">
        <van-icon name="user-circle-o" />
        <span>我的</span>
      </router-link>
    </ul>
  </div>
</template>

<script>
import { onMounted, computed } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { getStorage } from '@/common/js/utils'
export default {
  setup() {
    const route = useRoute()
    const store = useStore()
    onMounted(() => {
      const token = getStorage('token')
      const path = route.path
      if (token && !['/home'].includes(path)) {
        store.dispatch('updateCart')
      }
    })
    const cartItemCount = computed(() => {
      return store.state.cartItemCount
    })

    return {
      cartItemCount
    }
  }
}
</script>

<style lang="less" scoped >
.nav-bar {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  padding: 5px 0;
  z-index: 999;
  background: #fff;
  transform: translateZ(0);
  -webkit-transform: translateZ(0);
  .nav-list {
    width: 100%;
    display: flex;
    flex-direction: row;
    padding: 0;
    .nav-list-item {
      display: flex;
      flex: 1;
      flex-direction: column;
      text-align: center;
      color: #666;
      &.router-link-active {
        color: #EF3965;
      }
      i {
        text-align: center;
        font-size: 22px;
      }
      span {
        font-size: 12px;
      }
      .van-icon-shopping-cart-o {
        margin: 0 auto;
        margin-bottom: 2px;
      }
    }
  }
}
</style>
